# -*- encoding:utf-8 -*-
"""
-文件：address.py
-说明：地址类
-作者：tlj
"""
import hashlib
from binascii import hexlify, unhexlify
from .Base58 import *

class InvalidAddressString(Exception):
    pass


class AddressParamError(Exception):
    pass


class Address:
    ADDRESS_VERSION = 0

    def __init__(self, verify_key=None, address_str = None):

        if verify_key is not None:
            ripemd160 = hashlib.new('ripemd160')
            ripemd160.update(hashlib.sha256(verify_key.to_string()).digest())
            verify_key_hash = ripemd160.hexdigest()
            version = Address.ADDRESS_VERSION
            payload = '{:0>2x}'.format(version) + verify_key_hash

            # 验证码
            checksum = hashlib.sha256(hashlib.sha256((unhexlify(payload))).digest()).digest()[:4]

            self.address_str = base58_encode(payload+hexlify(checksum).decode('ascii'))

        elif address_str != None:
            if Address.is_valid_address_str(address_str):
                self.address_str = address_str
            else:
                raise InvalidAddressString()
        else:
            raise AddressParamError

    @staticmethod
    def is_valid_address_str(address_str):
        base58_decode_str = base58_decode(address_str)
        base58_decode_bytes = unhexlify(base58_decode_str)
        if len(base58_decode_bytes)<4:
            print('len<4')
            return False
        version = base58_decode_bytes[0]
        if version != Address.ADDRESS_VERSION:
            print('version error', version)
            return False

        checksum1 = hashlib.sha256(hashlib.sha256(base58_decode_bytes[:-4]).digest()).digest()[:4]
        checksum2 = base58_decode_bytes[-4:]
        if checksum2!=checksum1:
            print('checksum compare error', checksum1, checksum2)
            return False
        return True

    def get_address_str(self):
        return self.address_str

    def __eq__(self, other):
        return type(self) == type(other) and self.address_str == other.get_address_str()

    def __repr__(self):
        return self.address_str


if __name__ == '__main__':
    import ecdsa
    sk1 = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
    vk1 = sk1.get_verifying_key()
    a1 = Address(vk1)
    a2 = Address(vk1)
    print('a1',a1)
    print('a2',a2)

    assert(a1==a2)

    assert(Address.is_valid_address_str(a1.get_address_str()))

    sk2 = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
    vk2 = sk2.get_verifying_key()

    a3 = Address(vk2)
    a4 = Address(address_str='1GR3u7Lnq8NnHR8aW62h6shXishMTd6FCs')
    try:
        a5 = Address(address_str='12323')
    except InvalidAddressString:
        print('invalidaddressstring')

    print('a3',a3)
    assert(a3!=a1)
    assert (not Address.is_valid_address_str('2BU3EyZpAPethrchnNwZp86uZdQ6kzRHjN'))  # 非法的地址




